/**
 * 购物车
 * @author 康文程
 * @date 2024-1-3 下午 06:59
 */
class Shopping_cart {
    /**
     * 修改用户全部购物车商品是否选中状态
     * @param id 用户Id
     * @param status 状态
     * @return {Promise<boolean>} 布尔值
     */
    updateAllSelectedStatus(id,status){
        return new Promise((resolve, reject) => {
            $.ajax({
                url: "/www.xiaomi.com/modify/shopping/cart/all/status",
                method: "GET",
                data: {
                    id: id,
                    status:status
                },
                success: function (response) {
                    resolve(response);
                },
                error: function (error) {
                    reject(error);
                }
            });
        });
    }
    /**
     * 修改用户指定购物车商品是否选中状态
     * @param id 购物车Id
     * @param status 状态
     * @return {Promise<boolean>} 布尔值
     */
    updateSelectedStatus(id,status){
        return new Promise((resolve, reject) => {
            $.ajax({
                url: "/www.xiaomi.com/modify/shopping/cart/status",
                method: "GET",
                data: {
                    shoppingCartId: id,
                    isSelected:status
                },
                success: function (response) {
                    resolve(response);
                },
                error: function (error) {
                    reject(error);
                }
            });
        });
    }
    /**
     * 更新购物车中商品的数量
     *
     * @param shoppingCartId 购物车ID
     * @param quantity       商品数量
     * @return {Promise<boolean>} 布尔值
     * @throws SqlErrorException SQL执行异常
     */
    updateQuantity(shoppingCartId, quantity) {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: "/www.xiaomi.com/modify/shopping/cart/number",
                method: "POST",
                data: {
                    shoppingCartId: shoppingCartId,
                    quantity: quantity
                },
                success: function (response) {
                    resolve(response);
                },
                error: function (error) {
                    reject(error);
                }
            });
        });
    }

    /**
     * 添加购物车商品
     * @param informationId 用户编号
     * @param shoppingCartId 购物车编号
     * @param commodityId 商品Id
     * @return {Promise<boolean>} true添加成功 false添加失败
     */
    addToShoppingCart(informationId,shoppingCartId,commodityId) {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: "/www.xiaomi.com/add/shopping/cart",
                method: "POST",
                data: {
                    informationId: informationId,
                    shoppingCartId: shoppingCartId,
                    commodityId:commodityId
                },
                success: function (response) {
                    resolve(response);
                },
                error: function (error) {
                    reject(error);
                }
            });
        });
    }

    /**
     * 根据Id删除购物车商品
     * @param shoppingCartId 购物车Id
     * @return {Promise<boolean>} true删除成功 false删除失败
     */
    deleteFromShoppingCart(shoppingCartId){
        return new Promise((resolve, reject) => {
            $.ajax({
                url: "/www.xiaomi.com/delete/shopping/cart",
                method: "GET",
                data: {
                    shoppingCartId: shoppingCartId
                },
                success: function (response) {
                    resolve(response);
                },
                error: function (error) {
                    reject(error);
                }
            });
        });
    }
    /**
     * 添加购物车商品
     * @param insuranceIds 保险Ids
     * @param shoppingCartId 购物车编号
     * @return {Promise<boolean>} true修改成功 false修改失败
     */
    updateInsuranceIds(shoppingCartId,insuranceIds) {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: "/www.xiaomi.com/modify/shopping/cart/insurance/ids",
                method: "POST",
                data: {
                    shoppingCartId: shoppingCartId,
                    insuranceIds:insuranceIds
                },
                success: function (response) {
                    resolve(response);
                },
                error: function (error) {
                    reject(error);
                }
            });
        });
    }

    /**
     * 获取购物数量
     * @param informationId 用户信息Id
     * @return {Promise<int>} 购物车数量
     */
    getShoppingCart(informationId){
        return new Promise((resolve, reject) => {
            $.ajax({
                url: "/www.xiaomi.com/shopping/cart/number",
                method: "GET",
                data: {
                    informationId:informationId
                },
                success: function (response) {
                    resolve(response);
                },
                error: function (error) {
                    reject(error);
                }
            });
        });
    }
    /**
     * 获取购物数量
     * @param shoppingcart 实体类
     * @return {Promise<boolean>} true成功 false失败
     */
    getShoppingCartByCommodityIdAndInformationId(shoppingcart){
        return new Promise((resolve, reject) => {
            $.ajax({
                url: "/www.xiaomi.com/new/shopping/cart",
                method: "POST",
                data: {
                    shoppingCart:JSON.stringify(shoppingcart)
                },
                success: function (response) {
                    resolve(response);
                },
                error: function (error) {
                    reject(error);
                }
            });
        });
    }
}